package com.example.fragment.tool;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.fragment.R;


public class ToastUtils {

    Context CONTEXT_DIALOG;
    AlertDialog ad;

    public ToastUtils(Context context){
        CONTEXT_DIALOG = context;
    }

    public void DetectionDialog(String title, String message, View.OnClickListener trueOnClick,int style) {

        //新建Dialog
        AlertDialog.Builder ab = new AlertDialog.Builder(CONTEXT_DIALOG);
        View dialogview = View.inflate(CONTEXT_DIALOG, R.layout.dialog_layout, null);
        //得到Dialog上的控件
        TextView titleText = dialogview.findViewById(R.id.dialog_title);
        TextView messageText = dialogview.findViewById(R.id.dialog_messsge);
        ImageView truebn = dialogview.findViewById(R.id.positi_bn);
        ImageView falsebn = dialogview.findViewById(R.id.navige_bn);
        if(style==1) {
            falsebn.setVisibility(View.GONE);
        }
        titleText.setText(title);
        messageText.setText(message);
        ab.setView(dialogview);
        ad = ab.create();

        //调节Dialog大小并隐藏导航栏
        Utils.hideButton1(ad);
        ad.show();
        Utils.hideButton2(ad);
        WindowManager.LayoutParams wl = ad.getWindow().getAttributes();
//        wl.width = 390;
//        wl.height = 260;
        wl.width = 640;
        wl.height = 400;
        ad.getWindow().setAttributes(wl);
        ad.getWindow().setBackgroundDrawableResource(R.mipmap.dialog_background);

        //点击事件映射
        //两个按钮的取消窗口点击事件
        if(style==2) {
            truebn.setOnClickListener(trueOnClick);
            falsebn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ad.dismiss();
                }
            });
        }else{
            //一个按钮的取消窗口点击事件
            truebn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ad.dismiss();
                }
            });
        }
    }

    public void WrongDialog(String mess) {
        AlertDialog.Builder ab = new AlertDialog.Builder(CONTEXT_DIALOG);
        View wrongdialogview = View.inflate(CONTEXT_DIALOG, R.layout.wrong_dialog, null);
        ImageView close = wrongdialogview.findViewById(R.id.close_bn);
        TextView messa = wrongdialogview.findViewById(R.id.wrong_message);
        messa.setText(mess);
        ab.setView(wrongdialogview);
        ad = ab.create();
        Utils.hideButton1(ad);
        ad.show();
        Utils.hideButton2(ad);
        WindowManager.LayoutParams wl = ad.getWindow().getAttributes();
//        wl.width = 390;
//        wl.height = 300;
        wl.width = 640;
        wl.height = 400;
        ad.getWindow().setAttributes(wl);
        ad.getWindow().setBackgroundDrawableResource(R.mipmap.dialog_background);
        //关闭点击
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
    }

    public AlertDialog getDialog(){
        return ad;
    }
    public void setdismiss(){ad.dismiss();}
}
